<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Wallpapers_private extends wsFrontend
{

    public function __construct()
    {
        parent::__construct();
        if (strpos(strtolower($this->uri->uri_string()), 'wallpapers_private') !== false) {
            redirect(base_url());
        }
        $this->db->cache_set_path(dirname($this->db->cachedir) . '/Wallpapers_page/');
    }

    public function index($type = '')
    {
        $data = array();
        if(!$type)
            $type = 'latest';
        $filter['sort'] = $type;
        $data['type'] = $type;


        $filter['bypath'] = false;
        $this->load->model('wallpaper_model');

        $SearchReplace = array('{type}' => ucwords($type));

        $filter['show_disabled'] = FALSE;
        $filter['per_page'] = $config['per_page'] = $this->config->item('items_per_page');
        $page_number = (int)$this->input->get('page');
        $filter['offset'] = ($page_number ? ($page_number - 1) * $config['per_page'] : 0);

        if (isset($_GET['time']))
            $filter['time'] = $_GET['time'];


        if ($this->input->get('feed') == 'rss') {
            $this->load->helper('xml');
            $feed_data = array();
            $feed_data['title'] = $this->metadata->get_metadata('_wallpapers_page_title', 'wallpapers_page', $SearchReplace);
            $feed_data['description'] = $this->metadata->get_metadata('_wallpapers_page_description', 'wallpapers_page', $SearchReplace);
            $feed_data['posts'] = $this->wallpaper_model->getWallpapers($filter + array('rss' => true));
            header("Content-Type: application/rss+xml");
            echo clean_data($this->tpl->fetch('contents/rss', $feed_data));
            exit;
        }

        $data['total_count'] = $this->wallpaper_model->getWallpapers($filter + array('count' => true));

        //Initialize Pagination
        $config['base_url'] = $data['url'] = current_url();
        $config['total_rows'] = $data['total_count'];
        $config['custom_query_string'] = TRUE;
        $config['page_query_string'] = TRUE;
        $config['query_string_segment'] = 'page';

        if (!empty($_GET)) {
            $get_method = $this->frontend_helper->get_query_string(array('page'));
            $config['prefix'] = ($get_method ? $get_method . '&' : '?') . 'page=';
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['prefix'] = '?page=';
        }

        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['wallpapers'] = $this->wallpaper_model->getWallpapers($filter);



        $this->tpl->meta ['title'] = $this->metadata->get_metadata('_wallpapers_page_title', 'wallpapers_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['description'] = $this->metadata->get_metadata('_wallpapers_page_description', 'wallpapers_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['keywords'] = $this->metadata->get_metadata('_wallpapers_page_keywords', 'wallpapers_page', $SearchReplace, false, $page_number);


        $this->tpl->contents [] = $this->tpl->fetch('contents/wallpapers_page', $data);
        $this->tpl->render('layouts/home');
    }

}
